package com.nanhai.buddhist.college.module.study.controller.teacher;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.module.collegebase.api.student.vo.UpdatePasswordVO;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.TeacherApi;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.dto.TeacherRespDTO;
import com.nanhai.buddhist.college.module.study.controller.admin.attendanceleavereason.vo.AttendanceLeaveReasonRespVO;
import com.nanhai.buddhist.college.module.study.controller.admin.attendancemanagement.vo.AttendanceManagementListReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.attendancemanagement.vo.AttendanceManagementRespVO;
import com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord.vo.ClockRecordRespVO;
import com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord.vo.ClockRecordTodayReqVO;
import com.nanhai.buddhist.college.module.study.controller.student.attendance.vo.StudentManagementPageReqVO;
import com.nanhai.buddhist.college.module.study.controller.teacher.vo.AttendanceManagementAddReq;
import com.nanhai.buddhist.college.module.study.convert.attendanceleavereason.AttendanceLeaveReasonConvert;
import com.nanhai.buddhist.college.module.study.convert.attendancemanagement.AttendanceManagementConvert;
import com.nanhai.buddhist.college.module.study.convert.classclockrecord.ClockRecordConvert;
import com.nanhai.buddhist.college.module.study.dal.dataobject.attendanceleavereason.AttendanceLeaveReasonDO;
import com.nanhai.buddhist.college.module.study.dal.dataobject.attendancemanagement.AttendanceManagementDO;
import com.nanhai.buddhist.college.module.study.dal.dataobject.classclockrecord.ClockRecordDO;
import com.nanhai.buddhist.college.module.study.enums.ClockRoleTypeEnum;
import com.nanhai.buddhist.college.module.study.service.attendanceleavereason.AttendanceLeaveReasonService;
import com.nanhai.buddhist.college.module.study.service.attendancemanagement.AttendanceManagementService;
import com.nanhai.buddhist.college.module.study.service.classclockrecord.ClockRecordService;
import com.nanhai.buddhist.college.module.system.api.user.AdminUserApi;
import com.nanhai.buddhist.college.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

/**
 * @author AKai
 * @date 2022/8/24
 */
@Api(tags = "教师PC端 - 教师考勤")
@RestController
@RequestMapping("/study/teacher/attendance")
@Validated
public class TeacherManageController {

    @Resource
    private AttendanceManagementService attendanceManagementService;

    @Resource
    private ClockRecordService clockRecordService;

    @Resource
    private AttendanceLeaveReasonService attendanceLeaveReasonService;

    @Resource
    private TeacherApi teacherApi;

    @Resource
    private AdminUserApi adminUserApi;

    @PostMapping("/apply/leave")
    @ApiOperation("请假申请")
    public CommonResult<String> createAttendanceManagement(@Valid @RequestBody AttendanceManagementAddReq createReqVO) {
        createReqVO.setUserType(ClockRoleTypeEnum.TEACHER.getRole());
        return success(attendanceManagementService.createAttendanceManagement(createReqVO));
    }

    @GetMapping("/list")
    @ApiOperation("获取个人请假列表")
    public CommonResult<List<AttendanceManagementRespVO>> getAttendanceManagementListByQue(@Validated AttendanceManagementListReqVO reqVO) {
        reqVO.setUserType(ClockRoleTypeEnum.TEACHER.getRole()+"");
        List<AttendanceManagementDO> list = attendanceManagementService.getAttendanceManagementListByQue(reqVO);
        return success(AttendanceManagementConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获取请假分页")
    public CommonResult<PageResult<AttendanceManagementRespVO>> getAttendanceManagementPage(@Valid StudentManagementPageReqVO pageVO) {
        pageVO.setUserType(ClockRoleTypeEnum.TEACHER.getRole());
        PageResult<AttendanceManagementDO> pageResult = attendanceManagementService.getAttendanceManagementPage(pageVO);
        return success(AttendanceManagementConvert.INSTANCE.convertPage(pageResult));
    }


    @GetMapping("/getTodayClock")
    @ApiOperation("获取今日课程打卡详情")
    public CommonResult<List<ClockRecordRespVO>> getTodayClock(@Validated ClockRecordTodayReqVO recordTodayReqVO) {
        List<ClockRecordDO> todayClock = clockRecordService.getTodayClock(recordTodayReqVO);
        return success(ClockRecordConvert.INSTANCE.convertList(todayClock));
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除考勤管理")
    public CommonResult<Boolean> deleteAttendanceManagement(@RequestParam("id") String id) {
        attendanceManagementService.deleteAttendanceManagement(id);
        return success(true);
    }

    @GetMapping("/getLeaveReason")
    @ApiOperation("获取请假事由表")
    public CommonResult<List<AttendanceLeaveReasonRespVO>> getLeaveReasonAllList() {
        List<AttendanceLeaveReasonDO> list = attendanceLeaveReasonService.getAttendanceLeaveReasonAllList();
        return success(AttendanceLeaveReasonConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/get")
    @ApiOperation("获得考勤管理")
    public CommonResult<AttendanceManagementRespVO> getAttendanceManagement(@RequestParam("id") String id) {
        AttendanceManagementDO attendanceManagement = attendanceManagementService.getAttendanceManagement(id);
        return success(AttendanceManagementConvert.INSTANCE.convert(attendanceManagement));
    }

    @GetMapping("/teacherAllList")
    @ApiOperation("获取所有教师列表")
    public CommonResult<List<TeacherRespDTO>> getAllTeacherList(){
        return teacherApi.getAllTeacherList();
    }

    @GetMapping("/getTeacherBossUser")
    @ApiOperation("获取教师上属用户信息")
    public CommonResult<List<AdminUserRespDTO>> getTeacherAdminUserId(@RequestParam String id) {
        final TeacherRespDTO data = teacherApi.getTeacherById(id).getData();
        if (ObjectUtil.isEmpty(data)) {
            return success(new ArrayList<AdminUserRespDTO>());
        }
        return adminUserApi.getTeacherCheckUserByUser(data.getCollegeId());
    }


}
